<template>
	<view>
		<scroll-view class="home" scroll-y="true" :scroll-into-view="toView">
			<view class="flex align-center padding-tb-20 padding-lr-30">
				<view class="searchbg">
					<text class="tyIcon-search text-66"></text>
					<input type="text" placeholder="请输入地区名称" class="flex-one margin-left-20 text-32"
						confirm-type='search' @confirm="getSearch" v-model="name" />
				</view>
			</view>

			<view class="padding-30 flex align-center margin-bottom-40">
				<image src="/static/board/dw.png" mode="" class="dwimg"></image>
				<view class="flex-one text-32 margin-lr-10">{{city}}</view>
				<view class="dwbtn" @click="choseAdres">更新定位</view>
			</view>
			<view class="padding-tb-10 padding-lr-30 bg-e7 text-28 text-bold" v-if="searchreslut">
				搜索结果
			</view>
			<view @click="choseCity(searchreslut.name,searchreslut.id)" class="padding-30 text-32" v-if="searchreslut">
				{{searchreslut.name}}
			</view>
			<view class="padding-tb-10 padding-lr-30 bg-e7 text-28 text-bold">
				热门城市
			</view>
			<view @click="choseCity(item.name,item.id)" class="padding-30 border-bottom-ed text-32"
				v-for="(item,index) in hot" :key="index">
				{{item.name}}
			</view>

			<view class="" :id="'get' + index" v-for="(item,index) in list" :key="index">
				<view class="padding-tb-10 padding-lr-30 bg-e7 text-28 text-bold">
					{{item.key}}
				</view>
				<view class="padding-30 border-bottom-ed text-32" v-for="(items,indexs) in item.citylist" :key="indexs"
					@click="choseCity(items.name,items.id)">
					{{items.name}}
				</view>
			</view>


		</scroll-view>
		<view class="flex align-center flex-direction namelist">
			<!-- :class="index==0?'namebtn':''" -->
			<view class="width-40 text-center text-24 margin-bottom-5" v-for="(item,index) in list" :key="index"
				@click="choseKname(index)">
				{{item.key}}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				hot: [],
				list: [],
				toView: 'get0',
				city: "",
				city_id: "",
				name: "",
				searchreslut: ""
			}
		},
		onLoad() {
			this.city = uni.getStorageSync('city')
		},
		onReady() {
			this.rq.getData('Common/getAllCitiesByLetters').then(res => {
				if (res.code == 1) {
					this.hot = res.data.hot
					this.list = res.data.list
				}
			})
		},
		methods: {
			choseKname(index) {
				this.toView = 'get' + index
				console.log(this.toView)
			},
			choseAdres() {
				var that = this
				uni.chooseLocation({
					success: function(res) {
						uni.setStorageSync('lat', res.latitude)
						uni.setStorageSync('lng', res.longitude)
						that.rq.getData('common/getLocation', {
							latitude: res.latitude,
							longitude: res.longitude
						}).then(res => {
							if (res.code == 1) {
								uni.setStorageSync('city', res.data.city)
								uni.setStorageSync('city_id', res.city_id)
								that.city = res.data.city
								that.city_id = res.city_id
							}
						})
					}
				})
			},
			choseCity(name, id) {
				uni.setStorageSync('city', name)
				uni.setStorageSync('city_id', id)
				uni.navigateBack({
					delta: 1
				})
			},
			getSearch() {
				this.rq.getData('Common/find_city', {
					name: this.name
				}).then(res => {
					if (res.code == 1) {
						this.searchreslut = res.data
					}
				})
			}
		}
	}
</script>

<style>
	.home {
		width: 100%;
		height: calc(100vh - 10rpx);
		background: #fff;
		padding-bottom: 10rpx;
	}

	.searchbg {
		width: 100%;
		height: 80rpx;
		background: #F3F3F3;
		border-radius: 1998rpx 1998rpx 1998rpx 1998rpx;
		display: flex;
		align-items: center;
		padding: 0 20rpx;
	}

	.dwimg {
		width: 48rpx;
		height: 48rpx;
	}

	.dwbtn {
		padding: 10rpx 20rpx;
		background: #E7E7E7;
		border-radius: 40rpx;
		font-size: 32rpx;
		font-weight: bold;
	}

	.bg-e7 {
		background: #F3F3F3;
	}

	.namelist {
		position: fixed;
		right: 16rpx;
		bottom: 78rpx;
		z-index: 999;
	}

	.namebtn {
		width: 40rpx;
		height: 40rpx;
		background: #0052D9;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		font-size: 24rpx;
		color: #FFFFFF;
		line-height: 40rpx;
		text-align: center;
	}
</style>